#!/usr/bin/php
<?php

	$file_new = fopen("out.new", "r");
	$file_old  = fopen("out.old", "r");
	$file_out = fopen("out.diff", "w");
	
	$count = 0;
	
	$starting_address = "0xD5FA";
	
	//apontar para a primeira linha do new
      while(1){
            $line_new = fgets($file_new);
            $data_new = explode(":", $line_new);
            if($data_new[0] == "0xC070")
                  break;
            $count++;
      }
	
	//apontar para a primeira linha do old
      while(1){
            $line_old = fgets($file_old);
            $data_old = explode(":", $line_old);
            if($data_old[0] == "PC")
                  break;
      }	
	
	//For NEW
	if($starting_address != ""){
	      while(1){
                  $line_new = fgets($file_new);
                  $data_new = explode(":", $line_new);
                  if($data_new[0] == $starting_address)
                        break;
                  $count++;
	      }
	}
	
	//For OLD
	if($starting_address != ""){
	      while($line_old = fgets($file_old)){
	            $data_temp = explode("-", $line_old);
	            $data_temp[0] = explode(":", $data_temp[0]);
	            $data_temp[0] = trim($data_temp[0][1]);       
	            $data_temp[1] = explode(")", $data_temp[1]);
	            $data_temp[1] = trim($data_temp[1][1]);
	            $data_old = $data_temp;
	            
	            //print_r($data_old);
	            
                  if($data_old[0] == $starting_address)
                      break;              
	      }
	}	
	
	fprintf($file_out, "%s\n", $data_old[0] . ": " . $data_old[1]);
	
	while($line_old = fgets($file_old)){
		
            $data_temp = explode("-", $line_old);
            $data_temp[0] = explode(":", $data_temp[0]);
            $data_temp[0] = trim($data_temp[0][1]);       
            $data_temp[1] = explode(")", $data_temp[1]);
            $data_temp[1] = trim($data_temp[1][1]);
            $data_old = $data_temp;              
		
      	$line_new = fgets($file_new);

            $data_new = explode(":", $line_new);
            $data_new[0] = trim($data_new[0]);
            $data_new[1] = substr(trim($data_new[1]), 0, 3);       		

		
		if(($data_new[0] != $data_old[0]) || ($data_new[1] != $data_old[1])){
			fprintf($file_out, "DIFF:\n\tIn new: %s\n\n\tIn old: %s\n\n", 
			      $data_new[0] . ": " . $data_new[1], 
			      $data_old[0] . ": " . $data_old[1]);
			break;
		}
		else{
			$count++;
			fprintf($file_out, "%s\n", $data_new[0] . ": " . $data_new[1]);
		}
		
	}
	
	echo "Done. Last line (in new) that matched: ", $count, "\n";
	
	fclose($file_old);
	fclose($file_new);
	fclose($file_out);
	
?>
